//
//  ios_Director.mm
//  Prism
//
//  Created by Bas du Pre on 21-05-11.
//  Copyright 2011 UU. All rights reserved.
//

#include "global.h"
#if PRISM_OS_IOS
#include "ios_global.h"

#include "Director.h"

using namespace Prism;

void Director::native_init() {
    IOS_scene = [CCScene node];
    CGSize native_winSize = [[CCDirector sharedDirector] winSize];
    setWinSize(Prism::Size(native_winSize.width, native_winSize.height));
    IOS_frames = 0;
    IOS_accumDt = 0;
    IOS_dt = 0;
    IOS_lastFPS = 1.0 / [[CCDirector sharedDirector] animationInterval];
    onWinSizeChanged += delegate(this, &Director::native_updatePositionOnWinSizeChanged);
}

PFloat Director::native_fps() {
    if (IOS_frames == 0 || IOS_accumDt == 0)
        return IOS_lastFPS;
    
    IOS_lastFPS = IOS_frames/IOS_accumDt;
    IOS_frames = 0;
    IOS_accumDt = 0;
    
    return IOS_lastFPS;
}

void Director::native_sceneUpdate() {
    _scene->onUpdate += delegate(this, &Director::native_onUpdate);
}

void Director::native_onUpdate(void *sender, Prism::UpdateEventArgs *e) {
    IOS_dt = e->delta;
    IOS_accumDt += IOS_dt; 
    IOS_frames++;
}

void Director::native_beforeDrawScene() {
    
}

void Director::native_afterDrawScene() {
    
}

CCScene* Director::native_currentScene() {
    return IOS_scene;
}

void Director::native_updatePositionOnWinSizeChanged(void *sender, EventArgs *e) {
    if (runningScene() != 0)
        runningScene()->onPositionChange(sender, e);
    if (showDebugLayer()) 
        _debugLayer->onPositionChange(sender, e);
}

#endif
